<?php


namespace app\api\controller\v1;

use app\api\model\Image;
use app\api\model\UserAuth as UserAuthModel;
use app\api\model\UserAuthImg;
use app\api\model\UserPrivacy;
use LinCmsTp5\admin\model\LinFile;
use LinCmsTp5\exception\BaseException;
use think\Db;

class UserAuth
{
    /**
     * 获取列表
     * @validate('PagingParameter')
     */
    public function getDataList($title, $status,$page,$limit)
    {
        $where = [];
        if($title) {
            $where[] = ['title','like','%'.$title.'%'];
        }
        if($status) {
            $where[] = ['status','eq',$status];
        }
        $query = UserAuthModel::where($where)->with('imgs.img')->order('order_sort desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function (&$item){
            if($item['status'] == 1) {
                $item['status_name'] = '审核中';
            }elseif($item['status'] == 2) {
                $item['status_name'] = '审核通过';
            }elseif($item['status'] == 3) {
                $item['status_name'] = '审核不通过';
            }
            if($item['recommend'] == 0) {
                $item['recommend_name'] = '不推荐首页';
            } elseif($item['recommend'] == 1) {
                $item['recommend_name'] = '推荐首页';
            }
            if($item['state'] == 1) {
                $item['state_name'] = '未上墙';
            } elseif($item['state'] == 2) {
                $item['state_name'] = '已上墙';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取详情
     * @validate('IdMustBePositiveInt')
     */
    public function getDataInfo($id)
    {
        return UserAuthModel::where('id',$id)->with('imgs.img')->find();
    }

    /**
     * 审核通过信息
     * @validate('CheckGrActivity')
     */
    public function updateData($id,$recommend,$status,$status_con,$order_sort)
    {
        if($status == 2) {
            $status_con = '';
        }
        $info = UserAuthModel::get($id);
        if ($info) {
            $info->recommend = $recommend;
            $info->order_sort = $order_sort;
            $info->status = $status;
            $info->status_con = $status_con;
            $info->save();
            https_post("https://www.swparty.cn/tiancha/public/index.php/api/v1/msg/examine",['openid' => $info['openid']]);
        }
        return writeJson(202, [], '操作成功！');
    }


    /**
     * 上传信息
     *  1. 获取到数据, 转换数字
     *  2. 数组添加自定义openID
     *  3. 判断图片信息
     *  4. 添加信息用户认证表 - 认证图片表 - 隐私表
     *  5. 返回信息
     */
    public function createData($data)
    {
        // 获取数据库后台上传人数
        $count = UserAuthModel::where('type',2)->count();
        $count = $count + 1;
        Db::startTrans();
        try {
            foreach ($data as $k => $v) {
                if ($v['gender'] == '男') {
                    $v['gender'] = 1;
                } elseif ($v['gender'] == '女') {
                    $v['gender'] = 1;
                }
                if ($v['marriage'] == '未婚未育') {
                    $v['marriage'] = 1;
                } elseif ($v['marriage'] == '离婚未育') {
                    $v['marriage'] = 2;
                } elseif ($v['marriage'] == '离婚已育') {
                    $v['marriage'] = 3;
                }
                if ($v['education'] == '专科及以下') {
                    $v['education'] = 1;
                } elseif ($v['education'] == '本科') {
                    $v['education'] = 2;
                } elseif ($v['education'] == '硕士') {
                    $v['education'] = 3;
                } elseif ($v['education'] == '博士（含博士后）') {
                    $v['education'] = 4;
                }
                if ($v['house'] == '有房') {
                    $v['house'] = 1;
                } elseif ($v['house'] == '无房但有购买计划') {
                    $v['house'] = 2;
                } elseif ($v['house'] == '无房且无购买计划') {
                    $v['house'] = 3;
                }
                if ($v['car'] == '无') {
                    $v['car'] = 0;
                }
                if ($v['loan'] == '无') {
                    $v['loan'] = 0;
                } elseif ($v['loan'] == '房贷') {
                    $v['loan'] = 1;
                } elseif ($v['loan'] == '车贷') {
                    $v['loan'] = 2;
                } elseif ($v['loan'] == '车贷') {
                    $v['loan'] = 2;
                } else {
                    $v['loan'] = 3;
                }
                if ($v['contact_type'] == '手机号') {
                    $v['contact_type'] = 1;
                } elseif ($v['contact_type'] == '微信号') {
                    $v['contact_type'] = 2;
                } elseif ($v['contact_type'] == '微博号') {
                    $v['contact_type'] = 3;
                }
                if ($v['vip_status'] == 'TRUE') {
                    $v['vip_status'] = 0;
                } elseif ($v['vip_status'] == 'FALSE') {
                    $v['vip_status'] = 1;
                }
                $v['type'] = 2;
                $v['openid'] = 'obf3x' . ($count + $k + 1);
                $mode = UserAuthModel::create($v);
                UserPrivacy::create([
                    'openid' => $v['openid']
                ]);
                foreach ($v['imgs'] as &$vo) {
                    $file = LinFile::where('path', $vo['img']['path'])->find();
                    if ($file) {
                        $img = Image::where('file_id', $file['id'])->find();
                        UserAuthImg::create([
                            'user_auth_id' => $mode->id,
                            'img_id' => $img['id']
                        ]);
                    } else {
                        LinFile::create($vo);
                    }
                }
            }
            Db::commit();
            return writeJson(201, [], '添加成功！');
        }
        catch (\Exception $e){
                Db::rollback();
                throw new ParameterException([
                    'msg' => [
                        'user_auth' => '添加失败!'
                    ]
                ]);
            }
        return writeJson(201, [], '操作成功！');
    }
}